//
//  ChooseSizeVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/24.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "ChooseSizeVC.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "GoodsParam.h"
#import "Specval.h"
#import "UIImageView+WebCache.h"
#import "UIView+Category.h"
#import "MBProgressHUD.h"


#define MARGIN 15

@interface ChooseSizeVC () {
    int Number;           /**< 天机购物车数量  */
    NSString *specId;     /**< 添加购物车的参数 */
}
/** 具体参数数组*/
@property (strong, nonatomic) NSArray *specArr;
/** 顶部视图*/
@property (weak, nonatomic) IBOutlet UIImageView *topImage;

@property (weak, nonatomic) IBOutlet UIImageView *smallImage;
/** 价格*/
@property (weak, nonatomic) IBOutlet UILabel *priceLable;
@property (weak, nonatomic) IBOutlet UIButton *ReductionBtn;   /**< 减按钮 */
@property (weak, nonatomic) IBOutlet UIButton *AddBtn;         /**< 加按钮 */
@property (weak, nonatomic) IBOutlet UILabel *NumberLabel;     /**< 数量   */


@end

@implementation ChooseSizeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置xib视图
    [self setXibView];
    
    //获取参数数据
    [self getDataFromServer];
    
    Number = 1;
}

- (void)setXibView{
    /** 顶部视图*/
    
    //返回的字符串长这样---null/upload/goods/1439805780763.png
    NSString *str = [self.goods.DefaultImage substringFromIndex:4];
    //合并字符串
    NSString *imgStr = [NSString stringWithFormat:@"http://mall.yunku.mobi:8080/mall%@",str];
  
    [self.topImage sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"背景.png"]];

    /** 价格*/
    NSString * money=[NSString stringWithFormat:@"¥%f",self.goods.money];
    self.priceLable.text=[money substringToIndex:5];

}

/** 获取数据*/
- (void)getDataFromServer{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"id"] = @(self.goods.goodId);
    param[@"id"] = @19;//测试数据ID
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/agoods/spec.do"];
    [manager GET:path parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        int status = [responseObject[@"status"] intValue];
        if (status==1) {
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.HUD.mode = MBProgressHUDModeText;
            self.HUD.labelText = @"抱歉，没有规格可选";
            self.HUD.dimBackground = NO;
            
            [self.HUD hide:YES afterDelay:2];
        }else{
        
        NSArray *info = [GoodsParam objectArrayWithKeyValuesArray:responseObject[@"data"]];//该数组只有一个元素
        GoodsParam *specs = info[0];
        self.specArr = specs.specval;
        
        
        /**数据返回后显示到界面*/
        [self createSpecViews];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"----请求网络失败---%@",self.class);
    }];

    
}

/** 规格*/
- (void)createSpecViews{
    
    for (int i=0; i<self.specArr.count; i++) {
        UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(i%2*self.view.width/2+MARGIN, i/2*40+self.smallImage.y+self.smallImage.height+MARGIN, (self.view.width-3*MARGIN)/2, 100)];
        Specval * specval= self.specArr[i];
        lab.backgroundColor = [UIColor lightGrayColor];
        [UIView SetRoundedCorners:lab andcornerRadius:10.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
        
        NSString *string=[NSString stringWithFormat:@"容量:%@\n重量:%.2f\n售价:%.2f",specval.val,specval.weight,specval.sales];
        lab.text=string;
        lab.numberOfLines=0;
        lab.textColor=[UIColor whiteColor];
        lab.tag = 10+i;
        [self.view addSubview:lab];

        UIButton * btn=[UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame= lab.frame;
        btn.backgroundColor=[UIColor clearColor];
        btn.tintColor=[UIColor blackColor];
        btn.titleLabel.font =[UIFont systemFontOfSize:15];
        btn.tag=i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

}


/** 选择规格*/
- (void)btnClick:(UIButton *)button{
    UILabel *selectedLabel = (UILabel *)[self.view viewWithTag:10+button.tag];
    selectedLabel.backgroundColor = [UIColor orangeColor];
    for (int i = 0; i<self.specArr.count; i++) {
        if (i != button.tag) {
            UILabel *lab = (UILabel *)[self.view viewWithTag:10+i];
            lab.backgroundColor = [UIColor lightGrayColor];
        }
        
    }
    Specval *spec = self.specArr[button.tag];
    self.priceLable.text = [NSString stringWithFormat:@"¥%.2f",spec.sales];
    //发送通知当前选中的规格--规格改变
    //取出加入购物车所需的参数id
    specId = spec.spec_val_id;
    
}


- (IBAction)sureBtnClick:(UIButton *)sender {
    
    //发送通知
    if (!specId) {
        [self showText:@"请选择规格"];
        return;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SpecDidChange" object:nil userInfo:@{@"SelectedCitySpecId":specId,@"Number":[NSString stringWithFormat:@"%d",Number]}];
    [self dismissViewControllerAnimated:YES completion:nil];
    //返回上一级
    [self popViewController];
}

- (IBAction)ReductionBtnAction:(id)sender {
    if (Number <= 1) {
        return;
    }
    Number --;
    self.NumberLabel.text = [NSString stringWithFormat:@"%d",Number];
}

- (IBAction)AddBtnAction:(id)sender {
    
    Number ++;
    self.NumberLabel.text = [NSString stringWithFormat:@"%d",Number];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
